package network.tcp.nio;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * @program: baseModule
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-27 04:57
 **/
public class NioMethod {
    public static void main(String[] args) {
        // client1();
        // client2();
        // client3();
        // client5();
        client6();
    }

    private static void client6() {
        ByteBuffer buffer = ByteBuffer.allocate(10);
        buffer.put("abc".getBytes());

        //flip
        // System.out.println(buffer.limit());
        // buffer.flip();
        // System.out.println(buffer.limit());
        // System.out.println(buffer.capacity());
        //
        // //get
        // buffer.position(0);
        // System.out.println((char) buffer.get());
        // while (buffer.limit() != buffer.position()) {
        //     System.out.println((char) buffer.get());
        // }
        //
        // //
        // System.out.println("==========");
        // buffer.position(0);
        // byte[] bytes = new byte[buffer.limit()];
        // buffer.get(bytes);
        // System.out.println(new String(bytes));
        //
        // //
        // System.out.println("============");
        // System.out.println(buffer.get(2));
        // System.out.println(buffer.limit());
        //
        // System.out.println("===============");
        // buffer.rewind();
        // System.out.println(buffer.limit());
        // byte[] array = buffer.array();
        // System.out.println(Arrays.toString(array));
        //
        // System.out.println("===============");
        // //
        // System.out.println(buffer.limit());
        // buffer.clear();
        // System.out.println(buffer.limit());
        buffer.put("efg".getBytes());
        buffer.put("efg".getBytes());

        System.out.println("======flip=========");
        System.out.println(buffer.limit());
        System.out.println(buffer.position());
        buffer.flip();
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        byte[] array1 = buffer.array();
        System.out.println(Arrays.toString(array1));

        System.out.println("===============");
        //
        buffer.rewind();
    }

    private static void client5() {

        ByteBuffer buffer = ByteBuffer.wrap("123456".getBytes());
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        buffer.put((byte) 97);
        System.out.println("=================");
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        buffer.put("abc".getBytes());
        System.out.println("=================");
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        buffer.position(0);
        System.out.println("=================");
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());

        buffer.limit(5);
        System.out.println("=================");
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("=================");
        System.out.println(buffer.remaining());
        System.out.println(buffer.hasRemaining());
        System.out.println(buffer.capacity());
    }

    private static void client4() {
        ByteBuffer buffer = ByteBuffer.wrap("123456".getBytes());
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        buffer.put((byte) 97);
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());

        //
        System.out.println("======position修改=========");
        buffer.position(3);
        System.out.println(buffer.position());

        //
        System.out.println("======limit限制=========");
        System.out.println(buffer.limit());
        // ByteBuffer limit = buffer.limit(3);
        // System.out.println(limit);

        System.out.println("======capacity =========");
        System.out.println(buffer.capacity());

        System.out.println("======remaining=========");
        System.out.println(buffer.remaining());

        System.out.println("======hasRemaining=========");

        System.out.println(buffer.hasRemaining());
        System.out.println("======put=========");
        buffer.put((byte) 97);
        System.out.println(buffer);
        for (int i1 = 0; i1 < 3; i1++) {
            System.out.println(buffer.get());
        }
    }

    private static void client2() {
        ByteBuffer buffer = ByteBuffer.allocate(5);
        for (int i = 0; i < 5; i++) {
            System.out.println(buffer.get());
        }
        System.out.println(buffer.get());
    }

    private static void client3() {
        byte[] bytes = {97, 98, 99};
        ByteBuffer wrap = ByteBuffer.wrap(bytes);
        for (int i = 0; i < bytes.length; i++) {
            System.out.println((char) wrap.get());
        }
        System.out.println((char) wrap.get());

    }

    private static void client1() {
        //创建容量这么大的缓冲区

        //创建有内容的缓冲区

        //
        ByteBuffer wrap = ByteBuffer.wrap("abc".getBytes());
        for (int i = 0; i < 3; i++) {
            System.out.println((char) wrap.get());
        }
        for (int i = 0; i < 3; i++) {
            System.out.println((char) wrap.get());
        }
        System.out.println((char) wrap.get());
    }
}
